package com.lc.luzhengtang.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.bean.CommentBean;
import com.lc.luzhengtang.conn.Conn;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by congl on 2017/7/21.
 */

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {

    private Context context;
    private List<CommentBean> list;
    private OnItemClickListener listener;

    private int curPosition;

    public CommentAdapter(Context context, OnItemClickListener listener) {
        this.context = context;
        this.listener = listener;
        list = new ArrayList<>();
    }

    public void setDatas(List<CommentBean> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_new_evaluate, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.loadDatas(position);
        holder.ivPic.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private View good, mid, bad;
        private EditText content;
        private RecyclerView pics;
        private ImageView ivGood, ivMid, ivBad;
        private TextView tvGood, tvMid, tvBad;
        private ImageView ivPic;

        private MyTextWatcher textWatcher;

        public ViewHolder(View itemView) {
            super(itemView);
            ScaleScreenHelperFactory.getInstance().loadView(((ViewGroup) itemView));

            ivPic = ((ImageView) itemView.findViewById(R.id.iv_comment));

            good = itemView.findViewById(R.id.rl_good);
            mid = itemView.findViewById(R.id.rl_mid);
            bad = itemView.findViewById(R.id.rl_bad);

            content = ((EditText) itemView.findViewById(R.id.et_comment));
            textWatcher = new MyTextWatcher();

            pics = ((RecyclerView) itemView.findViewById(R.id.rv_pics));
            LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            pics.setLayoutManager(layoutManager);

            ivGood = ((ImageView) itemView.findViewById(R.id.iv_good));
            ivMid = ((ImageView) itemView.findViewById(R.id.iv_mid));
            ivBad = ((ImageView) itemView.findViewById(R.id.iv_bad));

            tvGood = ((TextView) itemView.findViewById(R.id.tv_good));
            tvMid = ((TextView) itemView.findViewById(R.id.tv_mid));
            tvBad = ((TextView) itemView.findViewById(R.id.tv_bad));

           /* ivMid.setSelected(true);
            tvMid.setSelected(true);*/
            ivGood.setSelected(true);
            tvGood.setSelected(true);

        }

        public void loadDatas(int position) {
            final CommentBean commentBean = list.get(position);
            CommentPicAdapter adapter = new CommentPicAdapter(context, new CommentPicAdapter.OnItemClickListener() {
                @Override
                public void onPicClick() {
                    listener.onAddPicClick(commentBean);
                }

                @Override
                public void onDeleteClick() {

                }
            });
            pics.setAdapter(adapter);
            adapter.setDatas(commentBean.getPicUrls());

            int evaluate = commentBean.getEvaluate();

            ivGood.setSelected(false);
            tvGood.setSelected(false);

            ivMid.setSelected(false);
            tvMid.setSelected(false);

            ivBad.setSelected(false);
            tvBad.setSelected(false);

            switch (evaluate) {
                case 1:
                    ivGood.setSelected(true);
                    tvGood.setSelected(true);
                    break;
                case 2:
                    ivMid.setSelected(true);
                    ivMid.setSelected(true);
                    break;
                case 3:
                    ivBad.setSelected(true);
                    tvBad.setSelected(true);
                    break;
                default:
                    ivGood.setSelected(true);
                    ivGood.setSelected(true);
            }


            Glide.with(context)
                    .load(Conn.HOST3 + commentBean.getPirUrl())
                    .error(R.mipmap.zhanwei)
                    .into(ivPic);

            good.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onGoodClick(commentBean);
                }
            });

            mid.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onMidClick(commentBean);
                }
            });

            bad.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onBadClick(commentBean);
                }
            });

            textWatcher.upadatePosition(position, content);
            content.setText(commentBean.getComment());
            content.setTag(position);
            content.addTextChangedListener(textWatcher);
//            holder.et2.setText(bean.getEt2());
            content.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    curPosition = ((int) v.getTag());
                    return false;
                }
            });

            if (curPosition == position) {
                content.requestFocus();
                content.setSelection(content.getText().length());
            } else {
                content.clearFocus();
            }
        }
    }

    public interface OnItemClickListener {
        void onGoodClick(CommentBean bean);

        void onMidClick(CommentBean bean);

        void onBadClick(CommentBean bean);

        void onAddPicClick(CommentBean bean);
    }

    class MyTextWatcher implements TextWatcher {

        private int position;
        private EditText editText;


        public MyTextWatcher() {
        }

        public void upadatePosition(int mPosition, EditText editText) {
            this.position = mPosition;
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            CommentBean bean = list.get(position);
            bean.setComment(editText.getText().toString());
        }
    }
}
